package com.jade.editor.object.editors.nodes;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;

public class Pole {
	
	public int x = 0;
	public int y = 0;
	
	private RGB _color = new RGB(0,0,0);
	private RGB _selectColor = new RGB(255, 0, 0);
	private Composite _parent = null;
	private NodeCollector _col = null;
	private Composite _point = null;
	private Pole thiz = null;
	private boolean _pressed = false;
	private int _mx = 0;
	private int _my = 0;
	private int _xb = 0;
	private int _yb = 0;
	private boolean _moveAllowed = false;
	
	public void setColor(RGB color){
		_color = color;
		_point.setBackground(SWTResourceManager.getColor(_color));
		_point.setForeground(SWTResourceManager.getColor(_color));
	}
	
	public void setSelectionColor(RGB color){
		_selectColor = color;
	}
	
	public Pole (Composite parent, NodeCollector col, int _x, int _y, RGB color, RGB selectColor){
		_parent = parent;
		_col = col;
		x = _x;
		y = _y;
		thiz = this;
		
		_color = color;
		_selectColor = selectColor;
		
		_point = new Composite(parent, SWT.NONE);
		_point.setBounds(x-2, y-2, 10, 10);
		_point.setBackground(SWTResourceManager.getColor(_color));
		
		Region r = new Region();
		int pointArray[] =  {5,0, 10,5, 5,10, 0,5};
		
		r.add(pointArray);
		_point.setRegion(r);
		addListeners();
	}
	
	public Pole(int _x, int _y){																		//+
		x = _x;																							//+
		y = _y;																							//+
	}
	
	public void init(Composite parent, NodeCollector col, RGB color, RGB selectColor){					//+
		_parent = parent;																				//+
		_col = col;																						//+
		thiz = this;																					//+
			
		_color = color;																					//+
		_selectColor = selectColor;																		//+
		
		_point = new Composite(parent, SWT.NONE);														//+
		_point.setBounds(x-2, y-2, 10, 10);
		_point.setBackground(SWTResourceManager.getColor(_color));										//+
		
		Region r = new Region();																		//+
		int pointArray[] =  {5,0, 10,5, 5,10, 0,5};														//+
		
		r.add(pointArray);																				//+
		_point.setRegion(r);																			//+
		addListeners();																					//+
	}
	
	
	public void setMoveAllowed(boolean b){
		_moveAllowed = b;
	}
	
	public void punish(){
		_point.dispose();
		thiz = null;
	}
	
	public void stepBack(){
		x = _xb + 2;
		y = _yb + 2;
		_point.setLocation(_xb, _yb);
		_parent.redraw();
	}
	
	private void addListeners(){
		_point.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				_point.setBackground(SWTResourceManager.getColor(_selectColor));
				_pressed = _pressed ? false : true;
				_mx = e.x;
				_my = e.y;
				_xb = _point.getLocation().x;
				_yb = _point.getLocation().y;
				_col.preSelect(thiz);
				
			}
			
			@Override
			public void mouseUp(MouseEvent e){
				_point.setBackground(SWTResourceManager.getColor(_color));
				_pressed = _pressed ? false : true;
				x = _point.getLocation().x + 2;
				y = _point.getLocation().y + 2;
				_col.select(thiz);
				_parent.redraw();
			}
		});
		
		_point.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if (_pressed && _moveAllowed){
					int xx = _point.getLocation().x + (e.x - _mx);
					int yy = _point.getLocation().y + (e.y - _my);
					_point.setLocation(xx, yy);
					x = xx + 2;
					y = yy + 2;
					_parent.redraw();	//TODO remove mb?
				}
				
			}
		});
		
	}

}
